package work07;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test07 {
    public static void print(File file){
        Map<String,Integer> map = new HashMap<>();
        int countDoc = 0;
        int countJava = 0;
        int countTxt = 0;
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()){
                if (files[i].getName().endsWith("doc")){
                    countDoc++;
                }
                else if (files[i].getName().endsWith("java")){
                    countJava++;
                }
                else if (files[i].getName().endsWith("txt")){
                    countTxt++;
                }
            }
        }
        map.put("pptx",countDoc);
        map.put("pdf",countJava);
        map.put("docx",countTxt);
        System.out.println("第一种方式遍历map集合如下：");
        for (Map.Entry<String,Integer>entry : map.entrySet()){
            System.out.println(entry.getKey() + "的类型文件有" + entry.getValue() + "个");
        }
        System.out.println();
        System.out.println("第二种方式遍历map集合如下：");
        map.forEach((k,v) -> System.out.println(k+"类型的文件有" + v + "个"));
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个文件夹路径：");
        String filePath = input.next();
        File file = new File(filePath);
        print(file);
    }
}
